package com.shanzhe.eshop.auth.service.Impl;

import com.shanzhe.eshop.auth.mapper.AuthPriorityMapper;
import com.shanzhe.eshop.auth.pojo.AuthPriorityDO;
import com.shanzhe.eshop.auth.pojo.AuthPriorityDTO;
import com.shanzhe.eshop.auth.pojo.AuthPriorityVO;
import com.shanzhe.eshop.auth.service.AuthPriorityService;
import com.shanzhe.eshop.common.util.ObjectUtils;
import com.shanzhe.eshop.common.util.ResultBean;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.ArrayList;
import java.util.List;

/**
 * @Author yinwei
 * @create 2021/10/2
 * @Version 1.0
 * @desc 权限管理模块service组件实现类
 */
@Service
@Slf4j
public class AuthPriorityServiceImpl implements AuthPriorityService {

    @Autowired
    AuthPriorityMapper authPriorityMapper;

    /**
     * 查询权限列表
     * @return List<AuthPriorityDO> 权限集合
     */
    @Override
    public ResultBean<List<AuthPriorityDTO>> listPriorityies(){

        try {
            List<AuthPriorityDO> authPriorityDOS = authPriorityMapper.listPriorityies();
            ArrayList<AuthPriorityDTO> authPriorityDTOS = new ArrayList<AuthPriorityDTO>(authPriorityDOS.size());

            for(AuthPriorityDO authPriorityDO:authPriorityDOS){
                authPriorityDTOS.add(authPriorityDO.clone(AuthPriorityDTO.class));
            }
            return ResultBean.success(authPriorityDTOS);
        }catch (Exception e){
            log.error("查询权限列表失败"+e.getMessage());
            e.printStackTrace();
            return ResultBean.error("查询权限列表失败"+e.getMessage());
        }

    }

    /**
     * 根据父权限id查询子权限
     * @param parentId
     * @return
     */
    @Override
    public ResultBean<List<AuthPriorityDTO>> listChildPriorities(Long parentId) {
        try {
            List<AuthPriorityDO> authPriorityDOS = authPriorityMapper.listChildPriorities(parentId);
            ArrayList<AuthPriorityDTO> authPriorityDTOS = new ArrayList<AuthPriorityDTO>(authPriorityDOS.size());

            for(AuthPriorityDO authPriorityDO:authPriorityDOS){
                authPriorityDTOS.add(authPriorityDO.clone(AuthPriorityDTO.class));
            }
            return ResultBean.success(authPriorityDTOS);
        }catch (Exception e){
            log.error("根据父权限id查询子权限失败"+e.getMessage());
            e.printStackTrace();
            return ResultBean.error("根据父权限id查询子权限失败"+e.getMessage());
        }

    }

    /**
     * 根据id查询权限
     * @param id 权限id
     * @return 权限
     */
    @Override
    public ResultBean<AuthPriorityDTO> getPriorityById(Long id) throws Exception {
        try {
            AuthPriorityDO authPriorityDO = authPriorityMapper.getPriorityById(id);
            AuthPriorityDTO authPriorityDTO = authPriorityDO.clone(AuthPriorityDTO.class);
            return ResultBean.success(authPriorityDTO);
        }catch (Exception e){
            log.error("根据id查询权限失败"+e.getMessage());
            e.printStackTrace();
            return ResultBean.error("根据id查询权限失败"+e.getMessage());
        }

    }

    @Override
    public ResultBean<List<AuthPriorityDTO>> listAuthorizedByAccountId(Long accountId) throws Exception {
        return null;
    }

    @Override
    public ResultBean<Boolean> existAuthorizedByCode(Long accountId, String code) throws Exception {
        return null;
    }

    @Override
    public ResultBean<Boolean> existAuthorizedByUrl(Long accountId, String url) throws Exception {
        return null;
    }

    /**
     * 新增权限
     * @param priorityDTO 权限DO对象
     */
    @Override
    public ResultBean<Boolean> savePriority(AuthPriorityDTO priorityDTO) throws Exception {
        try {
             AuthPriorityDO authPriorityDO = priorityDTO.clone(AuthPriorityDO.class);
             authPriorityMapper.savePriority(authPriorityDO);
             return ResultBean.success(true);
        }catch (Exception e){
            log.error("新增权限失败"+e.getMessage());
            e.printStackTrace();
            return ResultBean.error("新增权限失败"+e.getMessage());
        }

    }

    @Override
    public ResultBean<Boolean> updatePriority(AuthPriorityDTO priorityDTO) throws Exception {
        return null;
    }

    @Override
    public ResultBean<Boolean> removePriority(Long id) throws Exception {
        return null;
    }


}
